local skel = fk.CreateSkill {
  name = "lb__muji",
}

local DIY = require "packages.diy_utility.diy_utility"

Fk:loadTranslationTable{
  ["lb__muji"] = "沐殛",
  --[":lb__muji"] = "你可以使用一张本回合不因使用进入弃牌堆的牌，然后进行【闪电】判定。",
  --9.12
  [":lb__muji"] = "你可以使用一张本回合不因使用进入弃牌堆的牌（每回合每种牌名限一次），然后进行【闪电】判定。",

  ["@$lb__muji-turn"] = "沐殛",

  ["#lb__muji"] = "沐殛：你可以使用一张本回合不因使用进入弃牌堆的牌，然后进行【闪电】判定",

  ["$lb__muji1"] = "熟读医理，寻找药材，打开格局。",
  ["$lb__muji2"] = "还需辨疑去误呐。",
}

skel:addEffect("viewas", {
  pattern = ".",
  prompt = "#lb__muji",
  expand_pile = function(self, player)
    return player:getTableMark("lb__muji-turn")
  end,
  card_filter = function(self, player, to_select, selected)
    local card = Fk:getCardById(to_select)
    if #selected == 0 and table.contains(player:getTableMark("lb__muji-turn"), to_select)
    and not table.contains(player:getTableMark("@$lb__muji-turn"), card.trueName) then
      if Fk.currentResponsePattern == nil then
        return player:canUse(card)
      else
        return Exppattern:Parse(Fk.currentResponsePattern):match(card)
      end
    end
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 then return end
    return Fk:getCardById(cards[1])
  end,
  before_use = function (self, player, use)
    player.room:addTableMark(player, "@$lb__muji-turn", use.card.trueName)
  end,
  after_use = function (self, player, use)
    --闪电判定
    if player.dead then
      return
    end
    local room = player.room
    local pattern = ".|2~9|spade"
    local judge = {
      who = player,
      reason = "lightning",
      pattern = pattern,
    }
    room:judge(judge)
    if judge:matchPattern() then
      room:damage{
        to = player,
        damage = 3,
        damageType = fk.ThunderDamage,
        skillName = skel.name,
      }
    end
  end,
  enabled_at_play = function(self, player)
    return #player:getTableMark("lb__muji-turn") > 0
  end,
  enabled_at_response = function(self, player, response)
    return not response and #player:getTableMark("lb__muji-turn") > 0
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(skel.name, true) then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile and move.moveReason ~= fk.ReasonUse then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(player.room.discard_pile, info.cardId) then
              return true
            end
          end
        end
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.DiscardPile and not table.contains(player.room.discard_pile, info.cardId) then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local ids = player:getTableMark("lb__muji-turn")
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile and move.moveReason ~= fk.ReasonUse then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(room.discard_pile, info.cardId) then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
      for _, info in ipairs(move.moveInfo) do
        if info.fromArea == Card.DiscardPile and not table.contains(room.discard_pile, info.cardId) then
          table.removeOne(ids, info.cardId)
        end
      end
    end
    room:setPlayerMark(player, "lb__muji-turn", ids)
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  room:addSkill("#CenterArea")
  local ids = {}
  room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
    for _, move in ipairs(e.data) do
      if move.toArea == Card.DiscardPile and move.moveReason ~= fk.ReasonUse then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(room.discard_pile, info.cardId) then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
    end
  end, Player.HistoryTurn)
  room:setPlayerMark(player, "lb__muji-turn", ids)
end)

return skel
